/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var TgSearchTagModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`tag_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '关键词ID',
  //`name` varchar(20) NOT NULL DEFAULT '' COMMENT '关键词',
  //`type` int(2) NOT NULL DEFAULT '1' COMMENT '类别: '分类: 0未知 1单页, 2多页, 3ppt'
  //`order` int(3) NOT NULL DEFAULT '999' COMMENT '显示顺序',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',
};
TgSearchTagModel.prototype = new ItbBaseModel();

// 取得表名
TgSearchTagModel.prototype.getTableName = function(){
    return 'tg_search_tag';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
TgSearchTagModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'tag_id='+this.tag_id;
}

/**
 * 获取热门关键词的 sql
 * 参数: bForCnt 是否是获取个数
 * 参数: typeLs 逗号拼接的 typeL 字符串, 如: 1,2,3,4, 不能为空
 * */
function sql_getHotsSearchByTypeLs(bForCnt, typeLs){
    var sql = 'select ';
    sql += bForCnt ? 'count (distinct name) as count' : ' distinct name ';
    sql += "from tg_search_tag where 1=1 and type in ("+typeLs+") and del_flg=0";
    return sql;
}
/**
 * 根据 typeLs 获取对应分类的搜索热词, typeLs 为 对应分类的id 通过,链接, 如: 1,2,3
 *
 * */
TgSearchTagModel.prototype.getHotsSearchByTypeLs = function(typeLs, callback) {
    pool.pool.getConnection(function(err, connection) {
        var sql = sql_getHotsSearchByTypeLs(false, typeLs);
        console.log(sql);

        // Use the connection
        connection.query(sql, function(error, items) {
            // And done with the connection.
            connection.release();

            // 回调
            callback(error, items);
        });

    });


}


//--------------------------------end-------------------------------

// 导出
module.exports = TgSearchTagModel;